<?php 
/*
	@Author: Ninh The Anh - 
	Email: ninhtheanh@yahooo.com, ninhtheanh@gmail.com
	Phone number: +84983.68.7979
*/
class clsPaging
{ 	
	public $curPg;  
	public $maxRow = 20;
	public $maxPage = 5;  
	public $currentRow;  
	public $totalRows = 0;
	public $totalPages;  
	public $link;  
		
	public $table_name;
	public $field_id;
	public $sql;
	public $sql_sort;
	public $write_sql = false;
	public $sql_query_count;

	function __construct() 
	{
		if(is_numeric($this->maxRow))
			$this->maxRow = MAXROWS;
		if(is_numeric($this->maxPage))
			$this->maxPage = MAXPAGES;
		if($this->curPg == "")
			$this->curPg = (isset($_GET['curPg']) && is_numeric($_GET['curPg'])) ? $_GET['curPg'] : 1;
			
		$a = func_get_args(); 
        $i = func_num_args(); 
        if (method_exists($this,$f='__construct'.$i)) 
		{ 
            call_user_func_array(array($this,$f),$a); 
        } 	
	}
	function __construct2($sql, $first_sql) 
	{
		$this->first_sql = $first_sql;		
		$this->sql = $sql;		
	}
	function __construct3($sql, $first_sql, $sql_query_count)
	{
		$this->first_sql = $first_sql;		
		$this->sql = $sql;
		$this->sql_query_count = $sql_query_count;
	}
	function paging_query()
	{   
		$sql_count = $this->modified_query_count();		
		//echo '<br>sql_count:'.$sql_count;
		$result = $GLOBALS["db_mysql"]->query($sql_count); 
		$row = $GLOBALS["db_mysql"]->fetch_array($result);		
		$this->totalRows = $row[0];
		//echo $this->totalRows;
		if((int)$this->totalRows >= (int)$this->maxRow)
		{  
			$this->totalPages = ceil((int)$this->totalRows / (int)$this->maxRow);  
		}
		else
		{  
			$this->totalPages= 1;  
		}  
		//$this->curPg = (isset($_GET['curPg'])) && is_numeric($_GET['curPg']) ? $_GET['curPg'] : 1;
		if(empty($this->curPg) or (int)$this->curPg==1)
		{  
			$this->curPg = 1;  
			$this->currentRow = 0;  			
		}
		else
		{  
			$this->currentRow = ($this->curPg -1) * $this->maxRow;  
		}  
		return $this->modified_query();  
	}  
	function modified_query_count()
	{	
		if(isset($this->sql_query_count))
		{
			return $this->sql_query_count;
		}
		else
		{
			$sql_temp = strtolower($this->sql);	
			$tmp = substr($this->sql, strrpos($sql_temp,"from"));	//index of strpos, last index of: strrpos
			$sql_count = "Select COUNT(*) as items_count " . $tmp;
			//echo "<br>".$sql_count;
			return $sql_count;
		}
	}
	function modified_query()
	{  
		if($this->sql_sort != "")
			$query = $this->sql_sort;
		else
			$query = $this->sql ." LIMIT ".$this->currentRow.",".$this->maxRow;
		if($this->write_sql)
			echo "<br>".$query;		
		$result = $GLOBALS["db_mysql"]->query($query);
		//echo "<br>".$query;
		return $result;  
	} 
	function get_sql_sort($field_id = "", $arr_fields = NULL)
	{
		$this->field_id = $field_id;
		$strOrderBy = "";
		$sort_column = isset($_POST['hidden_sort_column']) ? $_POST['hidden_sort_column'] : "";
		if($sort_column != "")
		{
			list($NO, $sort) = explode(';', $sort_column);
			if($arr_fields != NULL && is_numeric($NO) && $arr_fields[$NO] != "")
			{
				$strOrderBy = " Order By " . $arr_fields[$NO] . " " . $sort;
			}
			else
			{
				$strOrderBy = " Order By " . str_replace(";", " ", $sort_column);
			}
			$str_id = isset($_POST['hidden_str_id']) ? $_POST['hidden_str_id'] : "";
			if($strOrderBy != "" && $str_id != "")
			{			
				return $this->first_sql . " And " . $this->field_id . " IN(". str_replace(";", ",", $str_id) .")" . $strOrderBy;
			}
		}	
		return "";
	}  
	function page_links($url = "", $viewAll = "")	  
	{	
		$current_url = $this->process_url($url);	
		$paging = "";	         		 
		$curPg = $this->curPg;
		
		$paging = "<table class='p12' align=\"right\" border=\"0\" cellpadding=\"2\" cellspacing=\"2\">";  
		$paging .= "<tr>";
		$paging .= "<td align=\"right\">Page: <b>" . $curPg . "</b> of <b>" . $this->totalPages . "</b> Records: <b>" . $this->totalRows . "</b></td>";
		if($this->totalPages > 1)
			$paging .= "<td align=\"right\" width='20px'></td>";
		if($this->totalRows > $this->maxRow)
		{
			$start = 1;
			$end = 1;
			$paging1 = "";				 	 
			for($i=1;$i<=$this->totalPages;$i++)
			{	
				if(($i>((int)(($curPg-1)/$this->maxPage))* $this->maxPage) && ($i<=((int)(($curPg-1)/$this->maxPage+1))* $this->maxPage))
				{
					if($start==1) $start=$i;
					if($i==$curPg)      
						$paging1 .=  "<td width='10px'><font color=red>" . $i. "</font></td>";
					else    
					{
						$paging1 .= "<td width='10px'><a href=\"" . $current_url . $i ."\" class='inactive'>$i</a></td>"; 
					}
					$end=$i;	
				}
			}
			if($curPg > $this->maxPage)
			{
				$paging .= "<td width='10px'><a href=\"" . $current_url . ($start-1)."\" class='inactive'>Previous</a></td>";  
			}			
			$paging.=$paging1;
			
			if($end < $this->totalPages)
			{
				$paging .= "<td width='10px'><a href=\"" . $current_url . ($end+1)."\" class='inactive'>Next</a></td>"; 
			}
			if($viewAll != "")
			{
				if(isset($_GET['type']) && $_GET['type'] == "all")
					$paging .=  "<td width='10px'><font color=red>All</font></td>";
				else
					$paging .= "<td width='10px'><a href=\"?type=all\" class='inactive'>All</a></td>"; 
			}
		}
				
		$paging .= "</tr></table>";
		return $paging;
	}
	function page_links_1($current_url)
	{  
		$current_url = $this->process_url($current_url);	
			
		$link = "<table class='p12' width='100%' align=\"left\" border=\"0\" cellpadding=\"2\" cellspacing=\"2\">";  
		$link .= "<tr>";  
		if($this->totalRows > $this->curPg)
		{  			
			if($this->curPg != 1)
			{  
				$link .=  "<td width='10px'><a href='" . $current_url . ($this->curPg - 1)."' class='inactive'><</a></td>";  
			}  
			for($ctr=1; $ctr <= (int)$this->totalPages; $ctr++)
			{  
				$link .= "<td width='10px'>";   
				if($this->curPg==$ctr)
				{  
					$link .=  "<font color=red>" . $ctr . "</font>";  
					$link .= "</td>";  
				}
				else
				{  
					$link .= "<a href='" . $current_url ."$ctr' class='inactive'>$ctr</a>";  
					$link .= "</td>";   
				}   
			}  
			if($this->curPg != $this->totalPages)
			{  
				$link .=  "<td width='10px'><a href='" . $current_url . ($this->curPg + 1)."' class='inactive'>></a></td>";  
			}
		}  				
		$link .= "<td align=\"right\">Page: <font color=red>" . $this->curPg . "</font> of <font color=red>" . $this->totalPages . "</font> Records: <font color=red>" . $this->totalRows . "</font></td>";   

		$link .= "</tr>";   
		$link .= "</table>";  
		return $link;  
	}	
	function page_links_2($current_url, $next_text = ">", $previous_text = "<")
	{  
		$current_url = $this->process_url($current_url);
			
		$link = "<table width='100%' class='p12' align=\"right\" border=\"0\" cellpadding=\"2\" cellspacing=\"2\">";  
		$link .= "<tr>";  
		if($this->totalRows > $this->curPg)
		{  			
			if($this->curPg != 1)
			{  
				$link .=  "<td align=\"left\"><a href='" . $current_url . ($this->curPg - 1)."' class='link_paging'>$previous_text</a></td>";  
			}  
			
			if($this->curPg != $this->totalPages)
			{  
				$link .=  "<td align=\"right\"><a href='" . $current_url . ($this->curPg + 1)."' class='link_paging'>$next_text</a></td>";  
			}
		}  				
		//$link .= "<td align=\"right\">Page: <font color=red>" . $this->curPg . "</font> of <font color=red>" . $this->totalPages . "</font> Records: <font color=red>" . $this->totalRows . "</font></td>";   
		$link .= "</tr>";   
		$link .= "</table>";  
		return $link;  
	} 
	function page_links_categories($current_url, $previous_text = "Previous", $next_text = "Next")
	{  
		$current_url = $this->process_url($current_url);	
			
		$link = '<div id="pagination">
								<div class="pagination-outer">
									<div class="pagination-inner">
										<ul>';
		if($this->totalRows >= $this->curPg)
		{  			
			if($this->curPg != 1)
			{  
				$link .=  '<li class="prev"><a href="'. $current_url . ($this->curPg - 1).'"> '.$previous_text.' </a></li>';
				//$link .=  "<td width='10px'><a href='" . $current_url . ($this->curPg - 1)."' class='inactive'><</a></td>";  
			}  
			for($ctr=1; $ctr <= (int)$this->totalPages; $ctr++)
			{  
				if($this->curPg==$ctr)
				{  
//					$link .=  "<font color=red>" . $ctr . "</font>";  
					$link .= '<li class="on"><a href="#">'.$ctr.'</a></li>';
				}
				else
				{  
					//$link .= "<a href='" . $current_url ."$ctr' class='inactive'>$ctr</a>";  
					$link .= '<li><a href="' . $current_url . $ctr . '">'.$ctr.'</a></li>';
				}   
			}  
			if($this->curPg != $this->totalPages)
			{  
				//$link .=  "<td width='10px'><a href='" . $current_url . ($this->curPg + 1)."' class='inactive'>></a></td>";  
				$link .=  '<li class="next"><a href="'. $current_url . ($this->curPg + 1).'"> '.$next_text.' </a></li>';
			}
		}  				

		$link .= "			</ul>
						</div>
					</div>
				</div>";  
		return $link;  
	}	
	function process_url($str)
	{	
		$str = urldecode($str);
		$str = str_replace("?type=all","",$str);
		if($str != "" && strpos($str, "?"))
		{
			$result = "";
			$str = str_replace("curPg", "temp", $str);
			$cmd = preg_split("/[?&]/", $str);
			$i = 0;
			foreach($cmd as $item)
			{
				if(!preg_match ("/^temp/", $item) || $i == 0) 
				{
					if(substr($item, strpos($item,".php")) == ".php")
						$result .= $item . "?";
					else
						$result .= $item . "&";	
				}	
				$i ++;	
			}
			$result = trim($result, "&");
			$result = trim($result, "?");			
			if(substr($result, strpos($result,".php")) == ".php")
				$result .=  "?curPg=";
			else
				$result .=  "&curPg=";
		}
		else
		{
			$result = basename($_SERVER['SCRIPT_FILENAME']) . "?curPg=";
		}		

		return $result;
	}
    
    function page_links_ul($url = "", $viewAll = "")      
    {    
        $current_url = $this->process_url($url);    
        $paging = "";                      
        $curPg = $this->curPg;
        $start_page = 1;
        $end_page = 1;
        
        if($curPg == 1)
        {
            $end_page = $this->maxPage;
        }
        else if($curPg == $this->totalPages)
        {
            $start_page = $this->totalPages - $this->maxPage;
            $end_page = $this->totalPages;
        }
        else
        {
            $midle_page = (int)($this->maxPage / 2);
            if($curPg - $midle_page <= 0)
            {
                $end_page = $this->maxPage;
            }
            else
            {
                if($curPg + $midle_page > $this->totalPages)
                {
                    $start_page = $this->totalPages - $this->maxPage;
                    $end_page = $this->totalPages;
                }
                else
                {
                    $start_page = $curPg - $midle_page;
                    $end_page = $curPg + $midle_page;
                }
            }
        }
        
        //hung
        //$end_page = $this->totalRows / $this->maxRow;
        //$end_page = is_float($end_page ) ? round($end_page + 1) : $end_page;
        
        $paging ="<ul>";
        //$paging .= "<li>Page: <b>" . $curPg . "</b> of <b>" . $this->totalPages . "</b> Records: <b>" . $this->totalRows . "</b></li>";
        if($curPg > 1)
        {
            $paging .= "<li class='prev'><a href='" . $current_url . ($curPg -1) . "'>&nbsp;</a></li>";
        }
        
        for($i= $start_page; $i <= $end_page; $i++)
        {
            if($i == $curPg)
            {
                $paging .= "<li class='active'><a>$i</a></li>";
            }
            else
            {
                $paging .= "<li><a href='" . $current_url . $i . "'>$i</a></li>";
            }
        }
        //skip step
        for($step = 1; $step < 3; $step++)
        {
            if($curPg + $this->maxPage * $step <= $this->totalPages)
            {
                $paging .= "<li><a href='" . $current_url . ($curPg + $this->maxPage * $step) . "'>" . ($curPg + $this->maxPage * $step) . "</a></li>";
            }
        }

        if($curPg + 1 <= $this->totalPages)
        {
            $paging .= "<li class='next'><a href='" . $current_url . ($curPg + 1) . "'>&nbsp;</a></li>";
        }
        $paging .= "</ul>";
        return $paging;
    } 
    
    function page_link_url_2($url = "", $viewAll = "")
    {
        $current_url = $this->process_url($url);    
        $paging = "";                      
        $curPg = $this->curPg;
        
        $paging = "<ul>";  
        
        if($this->totalRows > $this->maxRow)
        {
            $start = 1;
            $end = 1;
            $paging1 = "";                      
            for($i=1;$i<=$this->totalPages;$i++)
            {    
                if(($i>((int)(($curPg-1)/$this->maxPage))* $this->maxPage) && ($i<=((int)(($curPg-1)/$this->maxPage+1))* $this->maxPage))
                {
                    if($start==1) $start=$i;
                    if($i==$curPg)      
                        $paging1 .=  "<li class='active'><a>$i</a></li>";
                    else    
                    {
                        $paging1 .= "<li><a href=\"" . $current_url . $i ."\">$i</a></li>"; 
                    }
                    $end=$i;    
                }
            }
            if($curPg > $this->maxPage)
            {
                $paging .= "<li class='prev'><a href=\"" . $current_url . ($start-1)."\" class='inactive'>&nbsp;</a></td>";  
            }            
            $paging.=$paging1;
            
            if($end < $this->totalPages)
            {
                $paging .= "<li class='next'><a href=\"" . $current_url . ($end+1)."\" class='inactive'>&nbsp;</a></td>"; 
            }
            if($viewAll != "")
            {
                if(isset($_GET['type']) && $_GET['type'] == "all")
                    $paging .=  "<li class='view_all'>All</li>";
                else
                    $paging .= "<li><a href=\"?type=all\" class='inactive'>All</a></li>"; 
            }
        }
                
        $paging .= "<ul>";
        return $paging;
    } 
}
?>